package com.xuexiang.xui_lib.component.status;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.RoundProgressBar;
import ohos.agp.components.StackLayout;
import ohos.agp.utils.Color;
import ohos.app.Context;

import com.xuexiang.xui_lib.ResourceTable;

import java.util.ArrayList;
import java.util.List;

public class MultipleStatusComponent extends StackLayout {

    private static final LayoutConfig DEFAULT_LAYOUT_PARAMS = new LayoutConfig(LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_PARENT);

    public static final int STATUS_CONTENT = 0x00;
    public static final int STATUS_LOADING = 0x01;
    public static final int STATUS_EMPTY = 0x02;
    public static final int STATUS_ERROR = 0x03;
    public static final int STATUS_NO_NETWORK = 0x04;
    private static final int NULL_RESOURCE_ID = -1;

    private int mContentComponentId = -1;
    private int mEmptyComponentId;
    private int mErrorComponentId;
    private int mLoadingComponentId;
    private int mNoNetWorkComponentId;

    private int mComponentStatus = -1;

    private Component mEmptyComponent;
    private Component mErrorComponent;
    private Component mLoadingComponent;
    private Component mNoNetworkComponent;
    private Component mContentComponent;

    private LayoutScatter mLayoutScatter;
    private ClickedListener mOnRetryClickListener;
    private final List<Integer> mOtherIds = new ArrayList<>();

    public MultipleStatusComponent(Context context) {
        this(context, null);
    }

    public MultipleStatusComponent(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public MultipleStatusComponent(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initComponent(context);
    }

    private void initComponent(Context context) {
        mLayoutScatter = LayoutScatter.getInstance(context);
        mEmptyComponentId = ResourceTable.Layout_xui_msv_layout_empty;
        mErrorComponentId = ResourceTable.Layout_xui_msv_layout_error;
        mLoadingComponentId = ResourceTable.Layout_xui_msv_layout_loading;
        mNoNetWorkComponentId = ResourceTable.Layout_xui_msv_layout_no_network;
        showContent();
    }

    public final void showContent() {
        mComponentStatus = STATUS_CONTENT;
        if (null == mContentComponent && mContentComponentId > 0) {
            mContentComponent = mLayoutScatter.parse(mContentComponentId, null, false);
            addComponent(mContentComponent, 0, DEFAULT_LAYOUT_PARAMS);
        }
        showContentView();
    }


    public void setContentComponentId(int mContentComponentId) {
        this.mContentComponentId = mContentComponentId;
    }

    public void setEmptyComponentId(int mEmptyComponentId) {
        this.mEmptyComponentId = mEmptyComponentId;
    }

    public void setErrorComponentId(int mErrorComponentId) {
        this.mErrorComponentId = mErrorComponentId;
    }

    public void setNoNetWorkComponentId(int mNoNetWorkComponentId) {
        this.mNoNetWorkComponentId = mNoNetWorkComponentId;
    }

    public void setLoadingComponentId(int mLoadingComponentId) {
        this.mLoadingComponentId = mLoadingComponentId;
    }

    /**
     * getViewStatus
     *
     * @return 1
     */
    public int getViewStatus() {
        return mComponentStatus;
    }

    /**
     * 设置重试点击事件
     *
     * @param onRetryClickListener 重试点击事件
     */
    public void setOnRetryClickListener(ClickedListener onRetryClickListener) {
        mOnRetryClickListener = onRetryClickListener;
    }

    /**
     * 显示空视图
     */
    public final void showEmpty() {
        showEmpty(mEmptyComponentId, DEFAULT_LAYOUT_PARAMS);
    }

    /**
     * 显示空视图
     *
     * @param layoutId 自定义布局文件
     * @param layoutParams 布局参数
     */
    public final void showEmpty(int layoutId, LayoutConfig layoutParams) {
        showEmpty(inflateView(layoutId), layoutParams);
    }

    /**
     * 显示空视图
     *
     * @param component 自定义视图
     * @param layoutParams 布局参数
     */
    public final void showEmpty(Component component, LayoutConfig layoutParams) {
        mComponentStatus = STATUS_EMPTY;
        if (null == mEmptyComponent) {
            mEmptyComponent = component;
            if (null != mOnRetryClickListener) {
                mEmptyComponent.setClickedListener(mOnRetryClickListener);
            }
            mOtherIds.add(mEmptyComponent.getId());
            addComponent(mEmptyComponent, 0, layoutParams);
        }
        showViewById(mEmptyComponent.getId());
    }

    /**
     * 显示错误视图
     */
    public final void showError() {
        showError(mErrorComponentId, DEFAULT_LAYOUT_PARAMS);
    }

    /**
     * 显示错误视图
     *
     * @param layoutId 自定义布局文件
     * @param layoutConfig 布局参数
     */
    public final void showError(int layoutId, LayoutConfig layoutConfig) {
        showError(inflateView(layoutId), layoutConfig);
    }

    /**
     * 显示错误视图
     *
     * @param view 自定义视图
     * @param layoutConfig 布局参数
     */
    public final void showError(Component view, LayoutConfig layoutConfig) {
        checkNull(view, "Error view is null!");
        mComponentStatus = STATUS_ERROR;
        if (null == mErrorComponent) {
            mErrorComponent = view;
            if (null != mOnRetryClickListener) {
                mErrorComponent.setClickedListener(mOnRetryClickListener);
            }
            mOtherIds.add(mErrorComponent.getId());
            addComponent(mErrorComponent, 0, layoutConfig);
        }
        showViewById(mErrorComponent.getId());
    }

    /**
     * 显示加载中视图
     */
    public final void showLoading() {
        System.out.println("Loading" + "showLoading");
        showLoading(mLoadingComponentId, DEFAULT_LAYOUT_PARAMS);
    }

    /**
     * 显示加载中视图
     *
     * @param layoutId 自定义布局文件
     * @param layoutConfig 布局参数
     */
    public final void showLoading(int layoutId, LayoutConfig layoutConfig) {
        showLoading(inflateView(layoutId), layoutConfig);
    }

    /**
     * 显示加载中视图
     *
     * @param component 自定义视图
     * @param layoutConfig 布局参数
     */
    public final void showLoading(Component component, LayoutConfig layoutConfig) {
        checkNull(component, "Loading view is null!");
        mComponentStatus = STATUS_LOADING;
        if (null == mLoadingComponent) {
            mLoadingComponent = component;
            Component bar = mLoadingComponent.findComponentById(ResourceTable.Id_msv_progress);
            if (bar != null) {
                RoundProgressBar roundProgressBar = (RoundProgressBar) bar;
                roundProgressBar.setIndeterminate(true);
                roundProgressBar.setProgressColor(new Color(Color.getIntColor("#2A9CDC")));
            }
            mOtherIds.add(mLoadingComponent.getId());
            addComponent(mLoadingComponent, 0, layoutConfig);
        }
        showViewById(mLoadingComponent.getId());
    }

    /**
     * 显示无网络视图
     */
    public final void showNoNetwork() {
        showNoNetwork(mNoNetWorkComponentId, DEFAULT_LAYOUT_PARAMS);
    }

    /**
     * 显示无网络视图
     *
     * @param layoutId 自定义布局文件
     * @param layoutConfig 布局参数
     */
    public final void showNoNetwork(int layoutId, LayoutConfig layoutConfig) {
        showNoNetwork(inflateView(layoutId), layoutConfig);
    }

    /**
     * 显示无网络视图
     *
     * @param component 自定义视图
     * @param layoutParams 布局参数
     */
    public final void showNoNetwork(Component component, LayoutConfig layoutParams) {
        checkNull(component, "No network view is null!");
        mComponentStatus = STATUS_NO_NETWORK;
        if (null == mNoNetworkComponent) {
            mNoNetworkComponent = component;
            if (null != mOnRetryClickListener) {
                mNoNetworkComponent.setClickedListener(mOnRetryClickListener);
            }
            mOtherIds.add(mNoNetworkComponent.getId());
            addComponent(mNoNetworkComponent, 0, layoutParams);
        }
        showViewById(mNoNetworkComponent.getId());
    }


    private void showContentView() {
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            Component component = getComponentAt(i);
            component.setVisibility(mOtherIds.contains(component.getId()) ? HIDE : VISIBLE);
        }
    }

    private Component inflateView(int layoutId) {
        return mLayoutScatter.parse(layoutId, null, false);
    }

    private void showViewById(int viewId) {
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            Component view = getComponentAt(i);
            view.setVisibility(view.getId() == viewId ? VISIBLE : HIDE);
        }
    }

    private void clear(Component... components) {
        if (null == components) {
            return;
        }
        try {
            for (Component component : components) {
                if (null != component) {
                    removeComponent(component);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void checkNull(Object object, String hint) {
        if (null == object) {
            throw new NullPointerException(hint);
        }
    }
}
